package com.lzctzk.serviceaddress.controller;

import com.lzctzk.serviceaddress.domain.BuildingRepository;
import com.lzctzk.serviceaddress.domain.Building;
import com.lzctzk.serviceaddress.enums.ResultEnum;
import com.lzctzk.serviceaddress.exception.MyException;
import com.lzctzk.serviceaddress.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Title:
 * Description:
 * Company:
 *
 * @user GuiYue
 * @time 16:08
 * @project_name service-address
 * @package_name com.lzctzk.serviceaddress.controller
 **/

@Api("建筑物信息")
@RestController
@RequestMapping("/building")
public class BuildingController
{
    @Autowired
    private BuildingRepository buildingRepository;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @ApiOperation("新增建筑物地址信息")
    @ApiImplicitParam(name = "building", value = "单个建筑物信息实体", required = true, dataType = "Building")
    @PostMapping("")
    public Result insertOne(@RequestBody Building building) throws Exception
    {
        System.out.println(building.getCompDate());
        buildingRepository.save(building);
        return Result.ok();
    }

    @ApiOperation("获得建筑物地址信息")
    @ApiImplicitParam(name = "code", value = "建筑编码", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{code}")
    public Result getOne(@PathVariable("code") String code) throws Exception
    {
        Building building = buildingRepository.findById(code).orElse(null);
        if (building == null) {
            throw new MyException(ResultEnum.FULLERROR);
        }
        return Result.ok(building);
    }
}
